﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DPW.Business.Model;
using DPW.Entity;
using ETL.Common.Logging;

namespace DPW.Business.Repository
{
    public class CategoryRepository
    {
        public static List<PoemModel> GetPoemsOfCategory(int categoryId)
        {
            try
            {
                var db = new DPWEntities();
                var lstPoemModel = new List<PoemModel>();
                var poems = from p in db.Poems
                            where p.CategoryId == categoryId
                            orderby p.DateCreated descending 
                            select p;
                if (poems != null)
                {
                    var lstPoems = poems.ToList();
                    foreach (var poemEntity in lstPoems)
                    {
                        lstPoemModel.Add(new PoemModel(poemEntity));
                    }
                }
                return lstPoemModel;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                throw;
            }
        }

        public static CategoryModel AddNewCategory(string categoryName, string categoryDescription)
        {
            try
            {
                var db = new DPWEntities();
                
                var newCategory = new Category
                                    {
                                        CategoryId = GenerateNewCategoryId(),
                                        CategoryName = categoryName,
                                        CategoryDescription = categoryDescription
                                    };
                db.Categories.Add(newCategory);
                db.SaveChanges();
                return new CategoryModel(newCategory);

            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return null;
            }
        }

        public static int GenerateNewCategoryId()
        {
            try
            {
                var db = new DPWEntities();
                var category = (from c in db.Categories
                            orderby c.CategoryId descending 
                            select c).FirstOrDefault();

                if (category != null)
                {
                    return category.CategoryId + 1;
                }
                else return 1;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                throw;
            }
        }

        public static CategoryModel GetCategory(int categoryId)
        {
            try
            {
                var db = new DPWEntities();
                var category = (from c in db.Categories
                                where c.CategoryId == categoryId
                                select c).FirstOrDefault();

                if (category != null) return new CategoryModel(category);
                else return null;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                throw;
            }
        }

        public static List<CategoryModel> GetAllCategories()
        {
            try
            {
                var db = new DPWEntities();
                var models = new List<CategoryModel>();
                var categories = (from c in db.Categories
                                  select c);


                if (categories != null)
                {
                    foreach (var category in categories)
                    {
                        models.Add(new CategoryModel(category));
                    }
                    return models;
                }
                else return null;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                throw;
            }
        }

    }
}
